package com.wsy.servlet;

import com.wsy.pojo.Attendence;
import com.wsy.service.AttendenceService;
import com.wsy.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Slf4j
@WebServlet(name = "AddServlet",urlPatterns = "/addAttendence.html")
public class AddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String staffName=request.getParameter("staffName");
        Integer deptld=Integer.parseInt(request.getParameter("deptld"));
        Integer sex=Integer.parseInt(request.getParameter("sex"));
        String attendDate=request.getParameter("attendDate");
        String attendIn=request.getParameter("attendIn");
        String attendOut=request.getParameter("attendOut");
        String remark=request.getParameter("remark");
        ServletContext app=this.getServletContext();
        ApplicationContext ac= WebApplicationContextUtils.getWebApplicationContext(app);
        AttendenceService attendenceService= (AttendenceService) ac.getBean("attendenceService");
        Attendence attendence=new Attendence(0,sex,staffName,deptld,attendDate,attendIn,attendOut,remark,null);
        try {
            attendenceService.insert(attendence);
            response.sendRedirect(request.getContextPath()+"/index.html");
        }catch (Exception e){
            log.error("添加失败！",e);
            response.sendRedirect(request.getContextPath()+"/add.html");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
